package com.yan.retry;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;

public class UserFacadeImpl implements UserFacade {

	public void add() throws Exception {
		System.out.println("添加用户。。。");
		// throw new RuntimeException();
	}

	@Retryable(maxAttemps = 3)
	public void query() {
		System.out.println("查询用户。。。");
		throw new RuntimeException();
	}

	public Object newProxyInstance(Object target) {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(target.getClass());
		enhancer.setCallback(new AnnotationAwareRetryOperationsInterceptor());
		return enhancer.create();
	}

	public static void main(String[] args) throws Exception {

		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserFacadeImpl.class);
		enhancer.setCallback(new AnnotationAwareRetryOperationsInterceptor());
		UserFacade service1 = (UserFacade) enhancer.create();

		service1.query();

		Method method = Class.forName("com.yan.retry.UserFacadeImpl").getDeclaredMethod("query");
		// 从Method方法中通过方法getAnnotation获得我们设置的注解
		Retryable retryableAnnotation = method.getAnnotation(Retryable.class);

		// 得到注解的俩参数
		System.out.println(retryableAnnotation.maxAttemps());

	}

}
